package com.kmxd.ams.app.system;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.system.ISysMessageInfoSvc;
import com.kmxd.ams.client.system.ISysMessagesSvc;
import com.kmxd.ams.client.system.dto.SysMessagesAddDTO;
import com.kmxd.ams.client.system.dto.SysMessagesEditDTO;
import com.kmxd.ams.client.system.dto.SysMessagesQueryDTO;
import com.kmxd.ams.client.system.vo.SysMessagesListVO;
import com.kmxd.ams.client.system.vo.SysMessagesVO;
import com.kmxd.ams.core.common.enums.MessageStatusEnum;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.infra.system.entity.SysMessageInfo;
import com.kmxd.ams.infra.system.entity.SysMessages;
import com.kmxd.ams.infra.system.mapper.SysMessagesMapper;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统消息 服务实现类
 *
 * @author ZuoLG
 * @since 2022-07-15
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysMessagesSvcImpl extends ServiceImpl<SysMessagesMapper, SysMessages>
    implements ISysMessagesSvc {

  private SysMessagesMapper sysMessagesMapper;
  private ISysMessageInfoSvc sysMessageInfoSvc;

  @Override
  public Page<SysMessagesListVO> selectPage(Page reqPage, SysMessagesQueryDTO req) {
    LambdaQueryWrapper<SysMessages> queryWrapper =
        QueryGen.init(new QueryWrapper<SysMessages>(), req).lambda();
    Page<SysMessages> page = sysMessagesMapper.selectPage(reqPage, queryWrapper);
    Page<SysMessagesListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), SysMessagesListVO.class));
    return voPage;
  }

  @Override
  public Page<SysMessagesListVO> selectUserPage(Page reqPage, SysMessagesQueryDTO req) {
    List<SysMessagesListVO> list =
        sysMessagesMapper.selectUserPage(req, reqPage.offset(), reqPage.getSize());
    Long total = sysMessagesMapper.selectUserPageCount(req);
    Page<SysMessagesListVO> res = new Page<>(reqPage.getCurrent(), reqPage.getSize());
    res.setRecords(list);
    res.setTotal(total);
    return res;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(SysMessagesAddDTO req) {
    if (ObjectUtil.isAllEmpty(req.getSendType(), req.getReceiver())) {
      log.error("message add failed, 'sendType' and 'receiver' is null:{}", req);
      return;
    }

    SysMessages entity = BeanUtil.copyProperties(req, SysMessages.class);
    sysMessagesMapper.insert(entity);
    if (ObjectUtil.isNotEmpty(req.getReceiver())) {
      Set<Long> ids = IdUtil.split(req.getReceiver());
      List<SysMessageInfo> infoList =
          ids.stream()
              .map(
                  id -> {
                    SysMessageInfo info = new SysMessageInfo();
                    info.setMessageId(entity.getId());
                    info.setReceiveId(id);
                    info.setStatus(MessageStatusEnum.UNREAD.getType());
                    return info;
                  })
              .collect(Collectors.toList());
      sysMessageInfoSvc.saveBatch(infoList);
    }
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(SysMessagesEditDTO req) {
    SysMessages entity = BeanUtil.copyProperties(req, SysMessages.class);
    sysMessagesMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(String ids) {
    Set<Long> idList = IdUtil.split(ids);
    sysMessagesMapper.deleteBatchIds(idList);
  }

  @Override
  public SysMessagesVO view(Long id) {
    SysMessages entity = sysMessagesMapper.selectById(id);
    return BeanUtil.copyProperties(entity, SysMessagesVO.class);
  }
}
